/**
 * Created by z30 on 15-6-20.
 */

var https               =   require('https');
var http                =   require('http');
var tool                =   require('./t.js');

/*
 ezhttps
 */
exports.ezhttps=function(domain,method,uri,headers,body,cb)
{
    var resdata='';
    var options = {
        hostname: domain,
        port: 443,
        path: uri,
        method: method,
        headers:headers
    };

    var req = https.request(options, function(res) {
        //console.log(options)
        //console.log("statusCode: ", res.statusCode);
        //console.log("headers: ", res.headers);

        res.on('data', function(d) {
            resdata+=d;
        });

        res.on('end',function(){
            cb(null,resdata);
        });
    });
    req.write(JSON.stringify(body));
    req.end();
    req.on('error', function(err) {
        console.error(err,null);
    });
};


/*
 ezhttp
 */
exports.ezhttp=function(domain,method,uri,headers,body,cb)
{
    var resdata='';
    var options = {
        hostname: domain,
        port: 80,
        path: uri,
        method: method,
        headers:headers
    };

    var req = http.request(options, function(res) {
      // console.log(options);
        if(res.statusCode==200)
        {
          //  console.log(res)
            res.on('data', function(d) {
                //console.log(d)
                resdata+=d;
            });

            res.on('end',function(){
                cb(null,resdata);
            });
        } else
        {
            cb(null,JSON.stringify({'code':res.statusCode}));
        }
      //  console.log("statusCode: ", res.statusCode);
       // console.log("headers: ", res.headers);


    });
    req.write(body);
    req.end();
    req.on('error', function(err) {
        cb(err,null);
    });
};


/*
 ezhttp
 */
exports.ezhttp_engine=function(domain,method,uri,headers,body,cb)
{
    var resdata='';
    var options = {
        hostname: domain,
        port: 8000,
        path: uri,
        method: method,
        headers:headers
    };

    var req = http.request(options, function(res) {
        if(res.statusCode==200)
        {
            //  console.log(res)
            res.on('data', function(d) {
                //console.log(d)
                resdata+=d;
            });

            res.on('end',function(){
                cb(null,resdata);
            });
        } else
        {
            cb(null,JSON.stringify({'code':res.statusCode}));
        }
        //  console.log("statusCode: ", res.statusCode);
        // console.log("headers: ", res.headers);


    });
    req.write(body);
    req.end();
    req.on('error', function(err) {
        cb(err,null);
    });
};

/*
 ezhttp_ali
 */
exports.ezhttp_ali=function(domain,qsdict,cb)
{
//正式环境：http://gw.api.taobao.com/router/rest
//沙箱环境：http://gw.api.tbsandbox.com/router/rest
//app_key='23267743'
//app_sign='74c8cb525ff8e86a4da79c8d497351a7'
//AppKey 1023267743
//沙箱 AppSecret sandbox25ff8e86a4da79c8d497351a7

    var resdata='';
    var options = {
        hostname: domain,
        port: 80,
        path: '/router/rest?'+qsdict,
        method: 'GET',
        headers:{
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
        }
    };

    var req = http.request(options, function(res) {

         console.log('http://'+options['hostname']+options['path']);
        if(res.statusCode==200)
        {
            res.on('data', function(d) {
                resdata+=d;
            });

            res.on('end',function(){
                console.log(resdata)
                cb(null,resdata);
            });
        } else
        {
            cb(null,JSON.stringify({'code':res.statusCode}));
        }
        //  console.log("statusCode: ", res.statusCode);
        // console.log("headers: ", res.headers);
    });
    //req.write(JSON.stringify(body));
    req.end();
    req.on('error', function(err) {
        cb(err,null);
    });
};